// pages/user/index/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInformation: {},
    incomeStatistics: {},
  },
  showUserInfomation: function (e) {
    wx.navigateTo({
      url: '../userinformation/userinformation',
    })
  },
  notifyUserInfomation: function (e) {
    wx.navigateTo({
      url: '../notification/notification',
    })
  },
  setting: function (e) {
    wx.navigateTo({
      url: '../setting/setting',
    })
  },
  help: function (e) {
    wx.navigateTo({
      url: '../help/help',
    })
  },
  accountBtn: function () {
    wx.navigateTo({
      url: '../wallet/wallet',
    })
  },
  getUserInfo: function (e) {
    
    if (!e.detail.userInfo) {
      return
    }

    let _this = this;
    let userInfo = e.detail.userInfo;
    console.log(userInfo);

    var tmp_data = {
      avatarUrl: userInfo.avatarUrl,
      nickName: userInfo.nickName
    };
    if (userInfo.gender == 1) {
      tmp_data.gender = 17;
    } else if (userInfo.gender == 2){
      tmp_data.gender = 18;
    }

    wx.request({
      url: app.globalData.apiDomain + 'crowdsource/wechat/updateWechatUser',
      header: { 'Content-Type': 'application/json', 'token': app.globalData.token },
      method: 'POST',
      data: tmp_data,
      success: function (res) {
        if (res.data.status == 'ok') {

          // _this.data.userInformation.gender = tmp_data.gender ? tmp_data.gender : null;
          // _this.data.userInformation.avatarUrl = userInfo.avatarUrl;
          // _this.data.userInformation.nickName = userInfo.nickName;

          app.globalData.userInformation.gender = tmp_data.gender ? tmp_data.gender : null;
          app.globalData.userInformation.avatarUrl = userInfo.avatarUrl;
          app.globalData.userInformation.nickName = userInfo.nickName;

          _this.setData({
            userInformation: app.globalData.userInformation,
          })
        } else{
          app.responseCheck(_this, res.data, '/pages/user/index/index')
        }
      },
      fail: function (e) {
        wx.showModal({
          content: '网络异常,请重试',
          showCancel: false
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this;
    if (app.globalData.token == null) {
      app.login(_this, '/pages/user/index/index')
    } else {
      /*
      if (!_this.data.userInformation.overage) {
        _this.data.userInformation.overage = '0.00';
        _this.setData({
          userInformation: _this.data.userInformation,
        })
      }
      */
      _this.setData({
        userInformation: app.globalData.userInformation,
      })
      _this.getIncomeStatistics();
    }
    app.userLoginReadyCallback = res => {
      /*
      if (res.data.object.weChatUser && !res.data.object.weChatUser.overage) {
        res.data.object.weChatUser.overage = '0.00';
      }
      */
      _this.setData({
        userInformation: res.data.object.weChatUser,
      })
      _this.getIncomeStatistics();
    }

  },
  getIncomeStatistics: function () {
    let _this = this;
    wx.request({
      url: app.globalData.apiDomain + 'crowdsource/wechat/getIncomeStatistics',
      header: { 'Content-Type': 'application/json', 'token': app.globalData.token },
      success: function (res) {
        console.log(res.data.object);
        if (res.data.status == 'ok') {
          _this.setData({
            incomeStatistics: res.data.object,
          })
          app.globalData.incomeStatistics = res.data.object
        } else {
          app.responseCheck(_this, res.data, '/pages/user/index/index')
        }
      },
      fail: function (e) {
        wx.showModal({
          content: '网络异常,请重试',
          showCancel: false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }

})